package com.nmgydy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MobileInterceptor implements HandlerInterceptor {


	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		String rootPath = request.getContextPath();
		// 登陆验证
		String servletPath = request.getServletPath();
		String[] ignoreList = new String[] {
				"/mobile/login" // 登录界面
				,"/mobile/formLogin"
				,"/mobile/register"
				,"/mobile/registerTeam"
				,"/mobile/formRegister"
				,"/mobile/forget"
				,"/mobile/formForget"
				,"/mobile/get_check_code"
				,"/mobile/randCheckCode"//验证码
				,"/mobile/index"
				,"/mobile/list"
				,"/mobile/listData"
				,"/mobile/detail"
				,"/mobile/getUserType"
				,"/mobile/randCheckCode"
		};

		for (String path : ignoreList) {
			if (path.equals(servletPath)) {
				System.out.println(path);
				return true;
			}
		}

		String loginPage = "mobile/login";

		String sessionKey = "user";
		Object sessionObj = request.getSession().getAttribute(sessionKey);


		if (sessionObj == null  ) {
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-Type", "text/html;charset=UTF-8");
			response.sendRedirect(rootPath+"/"+loginPage);
			return false;
		}
		return true;

	}
}
